package com.longtop.framework.util;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;

/**
 * 
 * @author 胡砥峰
 * @version 2.0
 * 
 */
public class SpringBeanFactory {
	private static BeanFactory beanFactory = null;
	private static String beanFactoryId = "beanFactoryId";
	private static String springBeanFactoryFile = "/config/spring/springBeanFactory.xml";

	static {
		try {
			BeanFactoryLocator beanFactoryLocator = SingletonBeanFactoryLocator.getInstance(springBeanFactoryFile);
			BeanFactoryReference beanFactoryReference = beanFactoryLocator.useBeanFactory(beanFactoryId);
			beanFactory = beanFactoryReference.getFactory();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据beanId取得实例
	 * 
	 * @param beanId
	 * @return object
	 */
	public static <T> T getBean(String beanId) {
		return (T) beanFactory.getBean(beanId);
	}
}
